from flask import Blueprint, jsonify, request
import json
from db import db1

bp_cate = Blueprint('cate', __name__)


@bp_cate.route('/cate', methods=['POST', 'GET'])
def cate():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form

        sql = '''insert into cates values (0,"%s",%d,"%s")''' % (data['name'], int(data['pid']), "")
        db1.update(sql)
        return jsonify({'code': 200})
    elif request.method == 'GET':
        sql = '''select * from cates'''
        res = db1.findall(sql)
        return jsonify({'code': 200, 'list': res})
